package com.spring.boot.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import java.util.Iterator;
import java.util.List;

import java.util.UUID;

import javax.servlet.http.HttpServletRequest;


import org.apache.commons.httpclient.HttpException;


import com.alibaba.druid.stat.DruidStatManagerFacade;
import com.alibaba.druid.stat.DruidStatService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;

/**
 * 
 * @author ry
 *
 */
public class Helper {
	

	/**
	 * 获取配置的druid数据源
	 * 
	 * @return
	 * @throws HttpException
	 * @throws IOException
	 */
	public static List<String> druidDataSourceNumber() throws HttpException, IOException {
/*		HttpClient http = new HttpClient();
		//调用druid获取系统配置数据源
		PostMethod post = new PostMethod("http://127.0.0.1:8080/anta_restful_api/druid/datasource.json");
		post.getParams().setContentCharset("UTF-8");
		http.executeMethod(post);*/
		//解析json数据
		JSONObject object = JSON.parseObject(DruidStatService.returnJSONResult(1, DruidStatManagerFacade.getInstance().getDataSourceStatDataList()));
		//得到json数据中的数据源集合
		JSONArray array = object.getJSONArray("Content");
		List<String> list = new ArrayList<String>();
		//获取配置的数据源ID
		for (int i = 0; i < array.size(); i++) {
			list.add(array.getJSONObject(i).getString("UserName"));
		}
		return list;

	}

	/**
	 * 接收结果转换成jsonobject
	 * @param request
	 * @return
	 * @throws IOException
	 */
	public static JSONObject getjson(HttpServletRequest request) throws IOException{
		// 读取请求内容
		BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream(),"UTF-8"));
		String line = null;
		StringBuilder sb = new StringBuilder();
		while ((line = br.readLine()) != null) {
			sb.append(line);
		}
		// 将资料解码
		System.out.println(sb.toString());
		String reqBody =sb.toString();
		//JSON.parseObject(reqBody);
//		String reqBody = sb.toString();
		return  JSON.parseObject(reqBody,Feature.OrderedField);
	}
}
